MYFILE.TXT is an ordinary text file. You can use the Windows program NotePad with it, for example, or do anything with it you can with any text file.
QBasic programs can also create files which are not text files. These files are sometimes called binary files. These note will not discuss this.
An OPEN
statement must be executed before writing to or reading from a data file.
The statement looks like this:
OPEN file$ FOR mode AS filenumber
file$
is the name of the file, a DOS type file name.mode
isINPUT
orOUTPUT
.filenumber
is a number 1 through 255. Use a separate number for each file.
The file name should look like name.ext
where name
is
a name of one to eight characters and ext
is an extension of one to three
characters.
Here are some examples:
OPEN "DATA.DAT" FOR OUTPUT AS #1 OPEN OUTFILE$ FOR OUTPUT AS #200 OPEN "EMPLOYEE.DAT" FOR INPUT AS #2 OPEN NAME$ FOR INPUT AS #5
The next chapter will use the INPUT
mode.
The OPEN
statement can do much more than shown here.
It can open files for random access and work with networks, but
those features are not used in these notes.
What is wrong with the following program?
CLS ' Clear screen OPEN "MYFILE.TXT" FOR OUTPUT AS #1 ' Open the file for output OPEN "URFILE.TXT" FOR OUTPUT AS #1 ' Open the file for output PRINT #1, "Hello World" ' Send characters to the first file PRINT #1, "Hello Mars" ' Send characters to the second file END ' End the program